module.exports = ngModule => {
  ngModule.factory('loginService', ($q, $resource, Config) => {
    let list = [];
    let nextPage = 1;
    let pageNum = 0;
    let hasNextPage = true;

    let resource = $resource(Config.api.funds, {}, {
      query: {
        method: 'get',
        params: {
          pageNum: '@pageNum',
          pageSize: '@pageSize'
        },
        timeout: Config.timeout
      }
    });

    return {
      // 获取第一页数据
      refresh(pageSize) {
        let defer = $q.defer();
        resource.query({
          pageNum: 1,
          pageSize: pageSize,
        }, response => {
          if (response.info !== undefined) {
            pageNum = response.info.pageNum;
            hasNextPage = true;
            list = response.info.list;
            if (list.length < Config.pageSize) {
              hasNextPage = false;
            }
            nextPage = 2;
            defer.resolve(list);
          } else {
            defer.resolve(list);
          }
        }, error => {
          defer.reject(error);
        });
        return defer.promise;
      },
      loadMore() {
        if (list === undefined) {
          return false;
        }
        let defer = $q.defer();
        resource.query({
          pageNum: pageNum,
          pageSize: Config.pageSize
        }, response => {
          pageNum = response.info.pageNum;
          if (response.info.list.length < Config.pageSize) {
            hasNextPage = false;
          }
          list = list.concat(response.info.list);
          defer.resolve(list);
        }, error => {
          defer.reject(error);
        });
        return defer.promise;
      },
      hasNextPage() {
        return hasNextPage;
      }
    };
  });
};
